#!/usr/bin/python
# -*- coding: utf-8 -*-
#===============================================================================
# Copyright 2011 zod.yslin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
# Author: zod.yslin
# Email: 
# File Name: index.py
# Description: 
#   s.index(x[, i[, j]]),    return smallest k such that s[k] == x and i <= k < j

#   The index method does a linear search, and stops at the first matching item.
#   If no matching item is found, it raises a ValueError exception.
# Edit History: 
#   2011-08-15    File created.
#===============================================================================
L = [11,12,13,14,15,16,17,18,19]
print(L.index(15, 1, 5))
#print(L.index(15, 1, 4)) # ValueError: list.index(x): x not in list
#print(L.index(15, 5, 6)) # ValueError: list.index(x): x not in list

try:
    i = L.index(99)
except ValueError:
    i = -1 # no match

print i


def findall(L, value, start=0):
    # generator version
    i = start - 1
    try:
        i = L.index(value, i+1)
        yield i
    except ValueError:
        pass

print L
for index in findall(L, 13):
    print "match at", index
